package com.snail.readyvr.mvp.presenter;

import com.snail.readyvr.mvp.contract.PersonalCenterActivityContract;
import com.snail.readyvr.mvp.model.AccountInfo;
import com.snail.readyvr.rest.HttpResultFunc;
import com.snail.readyvr.rest.LoginRestClient;
import com.snail.readyvr.rest.RestApiUtils;
import com.snail.readyvr.util.Utils;

import javax.inject.Inject;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by MRKING on 2016/11/10.
 */

public class PersonalCenterActivityPresenter implements PersonalCenterActivityContract.Presenter {
    private PersonalCenterActivityContract.View mView;
    private LoginRestClient mRestClient;

    @Inject
    public PersonalCenterActivityPresenter(PersonalCenterActivityContract.View view, LoginRestClient restClient) {
        mView = view;
        mRestClient = restClient;
    }


    @Override
    public void start() {

    }

    @Override
    public void getAccountInfo(String token) {
        mRestClient.getAccountInfo(token).map(new HttpResultFunc<AccountInfo>(RestApiUtils.GET_ACCOUNT_INFO))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<AccountInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mView != null)
                            mView.error(e);
                    }

                    @Override
                    public void onNext(AccountInfo accountInfo) {
                        if(accountInfo != null) {
                            Utils.myAccountInfo = accountInfo;
                            if (mView != null)
                                mView.getAccountInfoSuccess();
                        }
                    }
                });
    }
}
